package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.BaseException;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class DishServiceImpl implements DishService {

    @Autowired
    DishMapper dishMapper;
    @Autowired
    DishFlavorMapper dishFlavorMapper;
    @Autowired
    SetmealDishMapper setMealDish;


    @Override
    @Transactional(rollbackFor = {BaseException.class}) //开启事务回滚
    public void addDish(DishDTO dishDTO) {

        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dish.setStatus(0);
        //添加菜品基本信息
        dishMapper.addDish(dish);

        //获得刚添加的菜品id
        Long dishId = dish.getId();
        //获得口味数组 并循环添加到数据库


        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && !flavors.isEmpty()) {
            //判断列表是否为空
            for (DishFlavor flavor : flavors) {
                //每个口味添加菜品id
                flavor.setDishId(dishId);
            }
        }
        //添加到数据库
        dishFlavorMapper.addFlavor(flavors);


    }

    @Override
    public PageResult dishPage(DishPageQueryDTO dishPageQueryDTO) {

        //开启分页查询
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        //查询
        Page<DishVO> page = dishMapper.dishPage(dishPageQueryDTO);
        //返回数据
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public DishVO getDishById(Long id) {

        return dishMapper.getDishById(id);

    }

    @Transactional(rollbackFor = {Exception.class}) //出现异常 回滚
    @Override
    public void editDish(DishDTO dishDTO) {

        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        //修改菜品基本信息
        dishMapper.editDish(dish);


        //修改菜品口味信息
        //先删除
        ArrayList<Long> dishFlavorId = new ArrayList<>();
        dishFlavorId.add(dish.getId());
        dishFlavorMapper.deleteDishFlavor(dishFlavorId);

        List<DishFlavor> flavors = dishDTO.getFlavors();

        if (flavors != null && !flavors.isEmpty()) {
            //判断列表是否为空
            for (DishFlavor flavor : flavors) {
                //每个口味添加菜品id
                flavor.setDishId(dishDTO.getId());
            }
        }
        //再添加
        dishFlavorMapper.addFlavor(flavors);

    }

    @Override
    public void deleteDish(List<Long> ids) {
        //判断菜品是否是起售中
        for (Long id : ids) {
            DishVO getDish = dishMapper.getDishById(id);
            if (getDish == null) {
                throw new NullPointerException("该菜品id不存在");
            }
            if (getDish.getStatus() == StatusConstant.ENABLE) {
                log.info("id为{}的商品在售 请先停售再删除", id);
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        //判断菜品是否被套餐关联
        List<Long> mealIds = setMealDish.getMealIdsByDishIds(ids);
        if (mealIds != null && !mealIds.isEmpty()) {
            log.info("所选菜品有被套餐关联");
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //删除dish表信息
        dishMapper.deleteDish(ids);
        //删除dish_flavor表信息
        dishFlavorMapper.deleteDishFlavor(ids);


    }

    @Override
    public List<DishVO> listWithFlavor(Dish dish) {

        /**
         * 条件查询菜品和口味
         * @param dish
         * @return
         */

        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d, dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

    @Override
    public List<Dish> list(Long categoryId) {

        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return dishMapper.list(dish);

    }


}
